package com.china08.yxyapi.api.manage.sch.platform.website.setting;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSettingLinks;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSettingLinksRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController("SwsSettingUpdLinksController")
public class SwsSettingUpdLinksController extends YxyApiBaseController {

    @Autowired
    private SwsSettingLinksRepository swsSettingLinksRepository;

    // 修改友情链接
    @ApiOperation("修改友情链接")
    @RequestMapping(value = "/api_yw/manage/sch/platform/website/setting/links/upt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object precess(HttpServletRequest request, HttpServletResponse response, @RequestBody(required = false) ReqModel reqModel) {

        List<SettingLink> links = reqModel.linkList;
        Assert.isTrue(null != links, "链接对象为空");
        for (SettingLink link : links) {
            String id = link.id;
            SwsSettingLinks settingLinks = swsSettingLinksRepository.findById(id);
            Assert.isTrue(null != settingLinks, "链接对象为空");
            settingLinks.setLinkName(link.linkName);
            settingLinks.setLinkUrl(link.linkUrl);
            settingLinks.setOrderNum(link.orderNum);
            swsSettingLinksRepository.save(settingLinks);

        }

        RespModel respModel = new RespModel();

        respModel.setMessage("成功");
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {
        public List<SettingLink> linkList;

        @Override
        public void autoCheck() {
            for (SettingLink settingLink : linkList) {
                String linkName = settingLink.linkName;
                String linkUrl = settingLink.linkUrl;
                Integer orderNum = settingLink.orderNum;

                Assert.isTrue(StringUtils.isNotBlank(linkName), "友情链接名称不能为空");
                Assert.isTrue(StringUtils.isNotBlank(linkUrl), "友情链接地址不能为空");

                boolean b1 = linkUrl.startsWith("http://");
                boolean b2 = linkUrl.startsWith("https://");
                Assert.isTrue(b1 || b2, "外链地址请以http://或https://开头");

                Assert.isTrue(orderNum != null, "序号不能为空");
                Assert.isTrue(orderNum > 0 && orderNum < 21, "序号只能输入1-20之间的整数");
            }
        }
    }

    public static class SettingLink {
        public String id;

        public String linkName;

        public String linkUrl;

        public Integer orderNum;
    }

    protected static class RespModel extends YxyApiRespModel {

    }
}
